package edu.hrbeu.crc.config;

import edu.hrbeu.crc.aiTools.BillTool;
import edu.hrbeu.crc.constant.SystemConstant;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {

    // 会话记忆
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory) {
        return ChatClient.builder(model) // 创建chatClient工厂
                .defaultSystem(SystemConstant.SYSTEM_CONSTANT)
                .defaultAdvisors(new SimpleLoggerAdvisor())// 记录日志，要在yml配置中配置好
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))// 添加会话记忆功能
                .build(); // 创建实例
    }

    @Bean
    public ChatClient helperChatClient(OpenAiChatModel model, ChatMemory chatMemory, BillTool billTool) {
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstant.SYSTEM_CONSTANT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .defaultTools(billTool)
                .build();
    }
}
